package com.lqq.eduorder.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lqq.commonutils.R;
import com.lqq.eduorder.entity.Order;
import com.lqq.eduorder.entity.OrderQuery;
import com.lqq.eduorder.entity.PayLog;
import com.lqq.eduorder.entity.PayQuery;
import com.lqq.eduorder.service.PayLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author 677
 * @since 2021-07-12
 */
@RestController
@RequestMapping("/eduorder/paylog")
//@CrossOrigin
public class PayLogController {

    @Autowired
    private PayLogService payLogService;

    //生成微信二维码，参数是订单号
    @GetMapping("/createNative/{orderNo}")
    public R createNative(@PathVariable("orderNo") String orderNo) {
        //返回信息，包含二维码地址，还有其他需要的信息
        Map map = payLogService.createNative(orderNo);
        System.out.println("-------------返回二维码map集合：" + map);
        return R.ok().data(map);
    }

    //查询订单支付状态,参数是订单号
    @GetMapping("/queryPayStatus/{orderNo}")
    public R queryPayStatus(@PathVariable("orderNo") String orderNo) {
        Map<String, String> map = payLogService.queryPayStatus(orderNo);
        System.out.println("----------------查询订单状态map集合：" + map);
        //支付出错
        if (map == null) {
            return R.error().message("支付出错了！");
        }
        //如果返回map不为空，通过map获取订单状态
        if (map.get("trade_state").equals("SUCCESS")) {
            //添加支付日志表记录，更改订单表的订单状态
            payLogService.updateOrderStatus(map);
            return R.ok().message("支付成功！");
        }
        return R.ok().code(25000).message("支付中...");
    }

    //分页条件查询
    @PostMapping("/list/{current}/{limit}")
    public R list(@PathVariable("current") Long current,
                  @PathVariable("limit") Long limit,
                  @RequestBody(required = false) PayQuery payQuery) {
        //分页
        Page<PayLog> page = new Page<>(current, limit);

        QueryWrapper<PayLog> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");
        //获取查询条件
        String orderNo = payQuery.getOrderNo();
        String transactionId = payQuery.getTransactionId();
        String payTime = payQuery.getPayTime();

        if (!StringUtils.isEmpty(orderNo)) {//加入条件1
            wrapper.eq("order_no", orderNo);
        }
        if (!StringUtils.isEmpty(transactionId)) {
            wrapper.eq("transaction_id", transactionId);
        }
        if (!StringUtils.isEmpty(payTime)) {
            wrapper.like("pay_time", payTime);
        }

        payLogService.page(page, wrapper);

        Map map = new HashMap();
        map.put("total", page.getTotal());
        map.put("items", page.getRecords());

        return R.ok().data("list", map);
    }

    //删除订单
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable String id) {
        boolean remove = payLogService.removeById(id);
        if (remove) {
            return R.ok().message("删除订单成功");
        }
        else {
            return R.ok().message("删除订单失败");
        }
    }

    //根据id查询交易记录
    @GetMapping("/getPayInfo/{id}")
    public R getPayInfo(@PathVariable("id") String id) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        PayLog payLog = payLogService.getById(id);
        return R.ok().data("item", payLog);
    }

    //编辑支付记录
    @PostMapping("/updatePay")
    public R updateOrder(@RequestBody PayLog payLog) {
        boolean update = payLogService.updateById(payLog);
        if (update) {
            return R.ok().message("修改支付记录成功");
        }else {
            return R.error().message("修改支付记录失败");
        }
    }


}

